package com.sendi.common.action;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sendi.common.service.ServiceSupport;
import com.sendi.common.util.Page;
import com.sendi.common.web.QueryInfo;
import com.sendi.system.service.ServiceSupportImp;

public class BaseAction extends ActionSupport {
	protected int start;
	protected int limit;
	protected String callback;
	protected QueryInfo queryInfo;
	protected DataSource dataSource;
	protected ServiceSupportImp serviceSupportImp;
	protected ServiceSupport serviceSupport;
	
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public String execute(){
		return "success";
	}
	public String getCallback() {
		return callback;
	}
	public void setCallback(String callback) {
		this.callback = callback;
	}
	public QueryInfo getQueryInfo() {
		return queryInfo;
	}
	public void setQueryInfo(QueryInfo queryInfo) {
		this.queryInfo = queryInfo;
	}
	public DataSource getDataSource() {
		return dataSource;
	}
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	public Connection getConnection(){
	    try{
	    	return dataSource.getConnection();
	    }
	    catch (SQLException e) {
	    	e.printStackTrace();
	    }
	    return null;
	}
	public HttpServletRequest getRequest(){
		ActionContext ctx = ActionContext.getContext();        
		HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);        
		return request;
	}
	
	public HttpServletResponse getResponse(){
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
		return response;
	}
	
	public void writeJSONObject(Object obj)throws Exception{
		HttpServletResponse response = getResponse();
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        String jsons = JSONArray.fromObject(obj).toString();
        if("[".equals(jsons.substring(0,1))){
        	jsons = jsons.substring(1, jsons.length());
        }
        if("]".equals(jsons.substring(jsons.length()-1))){
        	jsons = jsons.substring(0, jsons.length()-1);
        }
        System.out.println(jsons);
        response.getWriter().print(jsons);
	}
	public void writeJSONArray(List array)throws Exception{
		Page page = new Page();
		if(array != null){
			page.setTotalCount(array.size());
			page.setResults(array);
		}
		HttpServletResponse response = getResponse();
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        String jsons = JSONArray.fromObject(page).toString();
        if("[".equals(jsons.substring(0,1))){
        	jsons = jsons.substring(1, jsons.length());
        }
        if("]".equals(jsons.substring(jsons.length()-1))){
        	jsons = jsons.substring(0, jsons.length()-1);
        }
        System.out.println(jsons);
        response.getWriter().print(callback+"("+jsons+");");
	}
	public void writePage(Page page)throws Exception{
		HttpServletResponse response = getResponse();
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
		String jsons = JSONArray.fromObject(page).toString();
        if("[".equals(jsons.substring(0,1))){
        	jsons = jsons.substring(1, jsons.length());
        }
        if("]".equals(jsons.substring(jsons.length()-1))){
        	jsons = jsons.substring(0, jsons.length()-1);
        }
        System.out.println(jsons);
        if(StringUtils.isNotEmpty(callback)){
        	jsons = callback+"("+jsons+");";
        }
        response.getWriter().print(jsons);
	}
	public void writeResponseText(String responseText)throws Exception{
		HttpServletResponse response = getResponse();
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().print(responseText);
	}
	
	public void writeSession(String name,Object value)
	{
		HttpServletRequest request=this.getRequest();
		HttpSession session=request.getSession();
		session.setAttribute(name, value);
	}
	
	public Object readSession(String name)
	{
		HttpServletRequest request=this.getRequest();
		HttpSession session=request.getSession();
		Object object=session.getAttribute(name);
		return object;
	}
	public static String switchEncode(String oStr, String fromEncode, String toEncode)throws Exception
	{
		if(oStr!=null && oStr!="")
		{
			return new String(oStr.getBytes(fromEncode),toEncode);
		}
		
		return "";
	}
	public ServiceSupportImp getServiceSupportImp() {
		return serviceSupportImp;
	}
	public void setServiceSupportImp(ServiceSupportImp serviceSupportImp) {
		this.serviceSupportImp = serviceSupportImp;
	}
	public ServiceSupport getServiceSupport() {
		return serviceSupport;
	}
	public void setServiceSupport(ServiceSupport serviceSupport) {
		this.serviceSupport = serviceSupport;
	}
}
